import axios from '@/utils/request'
export default {
  ExchangeProduct: {
    entity: { name: 'Product', prefix: `manage` },
    form: {
      fields: [
        'name',
        {
          property: 'category',
          field_options: { label: '类型' },
          component: {
            props: ['form', 'property'],
            data() {
              return {
                categories: []
              }
            },
            created() {
              this.axios.get(`/manage/categories`, {
                params: {
                  '@filter': '!entity.getParent() && entity.getType().getSlug() == "ExchangePurchase"'
                }
              }).then((res) => {
                this.categories = res.data
              })
            },
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} filterable placeholder='类型'>{
                  this.categories.map(type => {
                    return <el-option-group label={type.name} key={type.id} value={type.id}>
                      {
                        type.children.map(category => {
                          return <el-option key={category.id} label={category.__toString} value={category.id} />
                        })
                      }
                    </el-option-group>
                  })
                }</el-select>
              )
            }
          }
        },
        { property: 'isOnSale', default_value: true },
        { property: 'cover', type: 'image' },
        { property: 'type', default_value: 1,type_options: { disabled: true }},
        { property: 'description', field_options: { label: '产品详情' } }
      ]
    },
    list: {
      // export: {
      //   query: {
      //     '@display': `{
      //       'stock' : 'entity.getStock().getSpecification().getName()',
      //       'quantity' : 'entity.getQuantity()',
      //       'unitPrice' : 'entity.getUnitPrice()',
      //       'comment' : 'entity.getComment()',
      //       'cretedTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")'
      //     }`,
      //     limit: 10000000
      //   },
      //   label: {
      //     'stock': '产品',
      //     'quantity': '数量',
      //     'unitPrice': '单价',
      //     'comment': '原因',
      //     'cretedTime': '时间'
      //   }
      // },
      // actions: [
      //   {
      //     name: '导入',
      //     position: 'top',
      //     component:
      //       () => import('@/views/product/synchronous.vue')
      //   }
      // ],
      query: {
        '@order': 'entity.id|DESC',
        '@filter': `entity.getCategory().getType().getSlug() == "ExchangePurchase"`,
        '@display': `{
          'id' : 'entity.getId()',
          'name': 'entity.getName()',
          'category': 'entity.getCategory()',
          'parentCategory': 'entity.getCategory() ? entity.getCategory().getParent().getName() : ""',
          'cover': 'entity.getCover()',
          'isOnSale': 'entity.getIsOnSale()',
          'isRecommended': 'entity.getIsRecommended()',
          'specifications': 'entity.getSpecifications()',
          'listOrder': 'entity.getListOrder()',
          'spu': 'entity.getSpu()',
          'tags': 'entity.getTags()'
        }`
      },
      list_filter: {
        'noneSpecification': {
          expression: 'entity.getSpecifications().count() == 0',
          label: '无规格产品',
          type: 'boolean'
        },
        'name': '名称',
        'category.id': () => {
          return axios
            .get('/api/categories',
              { params: { '@filter': 'entity.getParent() && entity.getType().getSlug() == "ExchangePurchase"', '@expands': "['entity.parent']" } })
            .then(res =>
              Object.assign(
                { __label: '产品分类' },
                ...res.data.map(v => { return { [v.id]: `${v.name}(${v.parent.__toString})` } })
              )
            )
        }
      },
      list_display: [
        { property: 'name', editable: true },
        { property: 'category', label: '类型' },
        { property: 'cover', type: 'image' },
        { property: 'isOnSale', editable: true, label: '上架' },
        {
          property: 'specifications',
          label: '规格',
          component: {
            props: ['data'],
            render(h) {
              return (
                <div>
                  {
                    this.data.map(v => {
                      return <p>{v.name}<span style='color:red'>(￥{v.price})</span></p>
                    })
                  }
                </div>
              )
            }
          }
        },
        { property: 'listOrder', editable: true }
      ]
    }
  }
}
